<?php
$this->sendDebugMessage("playCard model start");

$cid = $data->para->cardID;
$para = $data->para->para1;

$user = $this->userCenter->userList[$ip];
$tid = $user->tableID;
$rid = $user->roomID;
$tb = $this->userCenter->roomList[$rid]->tableList[$tid];

$pc = getCard($cid, $para);
$cardState = $tb->cardState;

if($tb->gameOver)
{
	$response = new Response();
	$response->event = "SE_PLAY_CARD";
	$response->body = '{"type":"turn","cardID":"'.$cid.'","userID":"'.$ip.'","seatID":"'.$user->seatID.'"}';
	$this->sendData($ip,$response);
	return;
}

include "checkUno.php";

if($user->seatID != $tb->currentPlayerSeatID)
{
	//抢出牌
	$response = new Response();
	$response->event = "SE_PLAY_CARD";
	$response->body = '{"type":"turn","cardID":"'.$cid.'","userID":"'.$ip.'","seatID":"'.$user->seatID.'"}';
	$this->sendData($ip,$response);
}
else
{
	if($pc->equal($cardState))
	{
		//正常出牌
		$pc->overlay($cardState);
		
		//计算下一个出牌者
		$nextSeatID = getNextPlayer($cardState,$tb);
		
		//出完牌后将stop引起的跳过重置
		$cardState->step = 1;
		
		//从玩家的牌堆中去掉打出的牌
		array_splice($tb->seatList[$user->seatID]->cardList, array_search($cid, $tb->seatList[$user->seatID]->cardList), 1);

		//通知出牌者
		$response = new Response();
		$response->event = "SE_PLAY_CARD";
		$response->body = '{"type":"normal","cardID":"'.$cid.'","userID":"'.$ip.'","seatID":"'.$user->seatID.'","nextSeatID":"'.$nextSeatID.'","state":'.$cardState.'}';
		$this->sendData($ip,$response);
		
		//通知其他的玩家更新
		$response->event = "SE_PLAY_CARD_UPDATE";
		$this->broadcastGroup($response,$this->userCenter->roomList[$user->roomID]->tableList[$user->tableID]->getUserList(),array($ip));
		
		//检查玩家是否胜利
		$seat = $tb->seatList[$user->seatID];

		if($seat->isWin())
		{
			$response = new Response();
			$response->event = "SE_GAME_OVER";
			$response->body = '{"type":"normal","winner":"'.$user->seatID.'","tableID":"'.$user->tableID.'","result":'.$tb->getResult().'}';
			$this->broadcastGroup($response,$this->userCenter->roomList[$user->roomID]->tableList[$user->tableID]->getUserList());
		}
		
	}else{
		//违反规则
		$user->uno = false;
		
		//罚牌
		$pcard = getPunishCard($tb,$user);
		
		//计算下一个出牌者
		$nextSeatID = getNextPlayer($cardState,$tb);
		
		//罚牌后重置
		$cardState->reset();
		$cardState->punished = true;
		
		$response = new Response();
		$response->event = "SE_PLAY_CARD";
		$response->body = '{"type":"rule","cardID":"'.$cid.'","userID":"'.$ip.'","seatID":"'.$user->seatID.'","nextSeatID":"'.$nextSeatID.'","card":["'.implode("\",\"",$pcard).'"]}';
		$this->sendData($ip,$response);
		
		$response->event = "SE_PLAY_CARD_UPDATE";
		$response->body = '{"type":"rule","cardID":"'.$cid.'","userID":"'.$ip.'","seatID":"'.$user->seatID.'","nextSeatID":"'.$nextSeatID.'","card":'.count($pcard).'}';
		$this->broadcastGroup($response,$this->userCenter->roomList[$user->roomID]->tableList[$user->tableID]->getUserList(),array($ip));
	}
	
	$tb->currentPlayerSeatID = $nextSeatID;
}

$this->sendDebugMessage("playCard model end");
